package io.gitee.chearnee.fw.sidecar.workshop;

import io.gitee.chearnee.fw.sidecar.application.ApplicationRegister;
import io.gitee.chearnee.fw.sidecar.model.BaseApplication;
import io.gitee.chearnee.fw.sidecar.model.BaseClientReq;
import io.gitee.chearnee.fw.sidecar.pool.SideCarThreadPool;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 12/19/2022 2:08 PM
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ActionProduce {
    private static BaseApplication baseApplication;

    public static <T extends BaseClientReq> void pushAction(T t) {
        SideCarThreadPool.getThreadPool().execute(() -> {
            buildApplication(t);
            if (!StringUtils.hasText(t.getServiceName())) {
                return;
            }
            WorkShop.add(t);
        });
    }

    public static <T extends BaseClientReq> void buildApplication(T t) {
        if (baseApplication == null) {
            return;
        }
        ApplicationRegister applicationRegister = baseApplication.getApplicationsMap().get(t.getAppId());
        if (applicationRegister == null) {
            return;
        }
        t.setServiceName(baseApplication.getServiceName());
    }

    public static <T extends BaseApplication> void init(T t) {
        if (baseApplication == null) {
            baseApplication = t;
        }
    }
}
